﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using NUS.ISS.ARS.Service.Entity;
using NUS.ISS.ARS.BusinessFacade.Interface;
using NUS.ISS.ARS.BusinessFacade;

namespace NUS.ISS.ARS.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class FlightBooking : IFlightBooking
    {
        private IAdminBF _adminBF;
        private IBookingBF _bookingBF;
        public FlightBooking()
        {
            _adminBF = new AdminBF();
            _bookingBF = new BookingBF();
        }

        public ICollection<FlightScheduleData> GetAllFlightSchedules()
        {
            ICollection<FlightScheduleData> result = new List<FlightScheduleData>();
            try
            {
                foreach (var data in _adminBF.GetAllFlightSchedules())
                {
                    result.Add(data);
                }
            }
            catch (Exception ex)
            {
                ServiceErrorException exception=new ServiceErrorException(){
                    ExceptionMessage=ex.Message,
                          InnerException=ex.InnerException!=null?ex.InnerException.Message:"",
                           StackTrace=ex.StackTrace,
                            Title="Service Error..."
                    };
                throw new FaultException<ServiceErrorException>(exception);
            }

            return result;
        }

        public SaveResultData CreateBooking(ReservationViewData data)
        {
            try
            {
                return _bookingBF.CreateBookings(DataAdapter.GetReservationViewFromReservationViewData(data));
            }
            catch (Exception ex)
            {
                ServiceErrorException exception = new ServiceErrorException()
                {
                    ExceptionMessage = ex.Message,
                    InnerException = ex.InnerException != null ? ex.InnerException.Message : "",
                    StackTrace = ex.StackTrace,
                    Title = "Service Error..."
                };
                throw new FaultException<ServiceErrorException>(exception);
            }
        }
    }
}
